/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.PhoneDao;
import com.techshop.model.Phone;
import java.util.List;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class PhoneDaoImpl extends HibernateDaoSupport implements PhoneDao {

    @Override
    public Phone getById(int productId) {
        try {
            return (Phone) getSession().get(Phone.class, productId);
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Phone> getAll() {
        try {
            return getSession().createQuery("FROM Phone").list();
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Phone product) {
        try {            
            getHibernateTemplate().save(product);
            return true;
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean update(Phone product) {
        try {
            if (getById(product.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().update(getHibernateTemplate().merge(product));
                ValidatePhonePubSubClient.updatePhone(product.getId());
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Phone product) {
        try {
            if (getById(product.getId()) == null) {
                return false;
            } else {
                getHibernateTemplate().delete(getHibernateTemplate().merge(product));
                ValidatePhonePubSubClient.updatePhone(product.getId());
                return true;
            }
        } catch (DataAccessResourceFailureException e) {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<Phone> search(String name, int category, int manufacturer)
    {
        try
        {
            if (category == 0)
            {
                if (manufacturer == 0)
                {
                    return getSession().createQuery("FROM Phone WHERE name like :name").setString("name", "%" + name + "%").list();
                }
                else
                {
                    return getSession().createQuery("FROM Phone WHERE name LIKE :name AND manufacturer.id=:manufacturerId").setString("name", "%" + name + "%").setInteger("manufacturerId", manufacturer).list();
                }
            }
            else
            {
                if (manufacturer == 0)
                {
                    return getSession().createQuery("FROM Phone WHERE name LIKE :name AND category.id=:categoryId").setString("name", "%" + name + "%").setInteger("categoryId", category).list();
                }
                else
                {
                    return getSession().createQuery("FROM Phone WHERE name LIKE :name AND category.id=:categoryId AND manufacturer.id=:manufacturerId").setString("name", "%" + name + "%").setInteger("categoryId", category).setInteger("manufacturerId", manufacturer).list();
                }
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public void updateQuantity(int phoneId, int quantity)
    {
        try
        {
            int updatedQuantity = getById(phoneId).getQuantity() + quantity;
            getSession().createSQLQuery("UPDATE phone SET quantity = :updatedQuantity WHERE id = :phoneId").setParameter("updatedQuantity", updatedQuantity).setParameter("phoneId", phoneId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
        }
    }

    @Override
    public void updateNumSlide(int phoneId, int numSlide)
    {
        try
        {
            int updatedNumSlide = getById(phoneId).getNumSlidePhoto() + numSlide;
            getSession().createSQLQuery("UPDATE phone SET num_slide_photo = :updatedNumSlide WHERE id = :phoneId").setParameter("updatedNumSlide", updatedNumSlide).setParameter("phoneId", phoneId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
        }
    }
}